﻿using Mybook.Models;
using MyStory.Service;
using MyStory.util;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Speech.Synthesis;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Mybook.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class BookContent : ContentPage
    {
        private FictionService _fictionService;
        FictionModel fictionModel;
        private string chapterid;
        List<FictionModel> repositorieschapter;
        ObservableCollection<string> chapteridreord;
        ObservableCollection<string> titlereord;
        int checkindex;
        int totel;

        public BookContent(FictionModel fictionModel, List<FictionModel> repositorieschapter)
        {
            chapterid = fictionModel.ChapterId;
            this.repositorieschapter = repositorieschapter;
            chapteridreord = new ObservableCollection<string>();
            titlereord = new ObservableCollection<string>();
            foreach (var item in repositorieschapter)
            {
                chapteridreord.Add(item.ChapterId);
                titlereord.Add(item.Title);
            }
            totel = chapteridreord.Count();
            InitializeComponent();
            BookContentasync(chapterid);
            title.Text = fictionModel.Title;

            Label1.FontSize = 17;
            toggle.IsVisible = false;

        }

        public async void BookContentasync(string chapterid)
        {

            _fictionService = new FictionService();
            string repositories = await _fictionService.GetContentRepositoriesAsync(ConnectUrl.GetContentUrl(chapterid));
            Label1.Text = repositories;
            checkindex = chapteridreord.IndexOf(chapterid);

        }

        private async void Button_Clicked_First(object sender, EventArgs e)
        {
            checkindex = 0;
            _fictionService = new FictionService();
            var chapteridnew = chapteridreord[checkindex];
            string repositories = await _fictionService.GetContentRepositoriesAsync(ConnectUrl.GetContentUrl(chapteridnew));
            Label1.Text = repositories;
            title.Text = titlereord[checkindex];
            //await scrollarea.ScrollToAsync(0,0,true);
            await scrollarea.ScrollToAsync(Label1, ScrollToPosition.Start, true);
        }
        private async void Button_Clicked_Pre(object sender, EventArgs e)
        {
            if (checkindex > 0)
            {
                checkindex--;
            }
            if (checkindex == 0)
            {
                checkindex = 0;
            }

            _fictionService = new FictionService();
            var chapteridnew = chapteridreord[checkindex];
            string repositories = await _fictionService.GetContentRepositoriesAsync(ConnectUrl.GetContentUrl(chapteridnew));
            Label1.Text = repositories;
            title.Text = titlereord[checkindex];
            //await scrollarea.ScrollToAsync(0,0,true);
            await scrollarea.ScrollToAsync(Label1, ScrollToPosition.Start, true);

        }
        private async void Button_Clicked_Next(object sender, EventArgs e)
        {
            if (checkindex < totel - 1)
            {
                checkindex++;
            }
            if (checkindex == totel - 1)
            {
                checkindex = totel - 1;
            }

            _fictionService = new FictionService();
            var chapteridnew = chapteridreord[checkindex];
            string repositories = await _fictionService.GetContentRepositoriesAsync(ConnectUrl.GetContentUrl(chapteridnew));
            Label1.Text = repositories;
            title.Text = titlereord[checkindex];
            //await scrollarea.ScrollToAsync(0,0,true);
            await scrollarea.ScrollToAsync(Label1, ScrollToPosition.Start, true);
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            toggle.IsVisible = !toggle.IsVisible;
        }
        private async void backPage(object sender, EventArgs e)
        {
            await Navigation.PopAsync();
        }
        /// <summary>
        /// tts语音
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        CancellationTokenSource cts;
        private async void sound_Page(object sender, EventArgs e)
        {
            cts = new CancellationTokenSource();
            var settings = new SpeechOptions()
            {
                Volume = (float?)volume.Value,
                Pitch = (float?)pitch.Value
            };
            //cancelToken: cts.Token,
            await TextToSpeech.SpeakAsync(Label1.Text, settings, cancelToken: cts.Token);
        }
        private void CancelSpeech(object sender, EventArgs e)
        {
            if (cts?.IsCancellationRequested ?? true)
                return;

            cts.Cancel();

        }


        // private async void Baidu_tts(object sender, EventArgs e)
        // {
        //     SpeechSynthesizer voice = new SpeechSynthesizer();   //创建语音实例
        //     voice.Rate = 3; //设置语速,[-10,10]
        //     voice.Volume = 100; //设置音量,[0,100]
        //     voice.SpeakAsync(Label1.Text);  
        // }

        ////滑动的下一页？？？？？Scrolled="scrollarea_Scrolled"
        //private async void scrollarea_Scrolled(object sender, ScrolledEventArgs e)
        //{
        //    Console.WriteLine($"111111111111111111{e.ScrollX}+++{e.ScrollY}===={scrollarea.ContentSize.Height-700}=={e.ScrollY - 20}");
        //    if (e.ScrollY >=scrollarea.ContentSize.Height-700 && scrollarea.ContentSize.Height -700>= e.ScrollY-20)
        //    {
        //        // scrollarea.ScrollToAsync(Label1, ScrollToPosition.End, true);
        //        Button_Clicked_Next(sender, e);
        //       // await scrollarea.ScrollToAsync(Label1, ScrollToPosition.Start, true);
        //        scrollarea.ScrollToAsync(0,0,true);


        //    }
        //}
    }
}